package com.zenithmind.task.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.task.pojo.entity.TaskLog;
import com.zenithmind.task.pojo.query.TaskLogQuery;
import com.zenithmind.task.pojo.vo.TaskLogVO;
import com.zenithmind.task.service.TaskLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 任务日志管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/task-log")
@RequiredArgsConstructor
@Tag(name = "任务日志管理", description = "任务日志的查询、创建、删除等功能")
public class TaskLogController {

    private final TaskLogService taskLogService;

    // ==================== 基础CRUD操作 ====================

    @GetMapping("/page")
    @Operation(summary = "分页查询任务日志列表", description = "支持多条件筛选的任务日志分页查询")
    public Result<PageResult<TaskLogVO>> getTaskLogPage(TaskLogQuery query) {
        String userId = UserContext.requireUserId();
        PageResult<TaskLogVO> result = taskLogService.getTaskLogPage(query, userId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询任务日志详情", description = "根据ID查询任务日志的详细信息")
    public Result<TaskLogVO> getTaskLogById(@Parameter(description = "日志ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        TaskLogVO taskLog = taskLogService.getTaskLogById(id, userId);
        return Result.success(taskLog);
    }

    @PostMapping
    @Operation(summary = "创建任务日志", description = "创建新的任务日志记录")
    public Result<Boolean> createTaskLog(@Valid @RequestBody TaskLog taskLog) {
        String userId = UserContext.requireUserId();
        Boolean result = taskLogService.createTaskLog(taskLog, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除任务日志", description = "删除指定的任务日志")
    public Result<Boolean> deleteTaskLog(@Parameter(description = "日志ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        Boolean result = taskLogService.deleteTaskLog(id, userId);
        return Result.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除任务日志", description = "批量删除多个任务日志")
    public Result<Boolean> batchDeleteTaskLogs(@RequestBody List<String> ids) {
        String userId = UserContext.requireUserId();
        Boolean result = taskLogService.batchDeleteTaskLogs(ids, userId);
        return Result.success(result);
    }

    // ==================== 日志记录操作 ====================

    @PostMapping("/status-change")
    @Operation(summary = "记录状态变更日志", description = "记录任务状态变更的日志")
    public Result<Boolean> logStatusChange(
            @Parameter(description = "任务ID") @RequestParam String taskId,
            @Parameter(description = "旧状态") @RequestParam Integer oldStatus,
            @Parameter(description = "新状态") @RequestParam Integer newStatus,
            @Parameter(description = "备注") @RequestParam(required = false) String remark) {
        String userId = UserContext.requireUserId();
        Boolean result = taskLogService.logStatusChange(taskId, oldStatus, newStatus, userId, remark);
        return Result.success(result);
    }

    @PostMapping("/assignment")
    @Operation(summary = "记录分配日志", description = "记录任务分配的日志")
    public Result<Boolean> logAssignment(
            @Parameter(description = "任务ID") @RequestParam String taskId,
            @Parameter(description = "被分配人ID") @RequestParam String assigneeId,
            @Parameter(description = "被分配人姓名") @RequestParam String assigneeName,
            @Parameter(description = "备注") @RequestParam(required = false) String remark) {
        String userId = UserContext.requireUserId();
        Boolean result = taskLogService.logAssignment(taskId, assigneeId, assigneeName, userId, remark);
        return Result.success(result);
    }

    @PostMapping("/progress-update")
    @Operation(summary = "记录进度更新日志", description = "记录任务进度更新的日志")
    public Result<Boolean> logProgressUpdate(
            @Parameter(description = "任务ID") @RequestParam String taskId,
            @Parameter(description = "旧进度") @RequestParam Integer oldProgress,
            @Parameter(description = "新进度") @RequestParam Integer newProgress,
            @Parameter(description = "备注") @RequestParam(required = false) String remark) {
        String userId = UserContext.requireUserId();
        Boolean result = taskLogService.logProgressUpdate(taskId, oldProgress, newProgress, userId, remark);
        return Result.success(result);
    }

    @PostMapping("/work-hours")
    @Operation(summary = "记录工时日志", description = "记录任务工时的日志")
    public Result<Boolean> logWorkHours(
            @Parameter(description = "任务ID") @RequestParam String taskId,
            @Parameter(description = "工时") @RequestParam Double hours,
            @Parameter(description = "工作描述") @RequestParam(required = false) String description) {
        String userId = UserContext.requireUserId();
        Boolean result = taskLogService.logWorkHours(taskId, hours, description, userId);
        return Result.success(result);
    }

    // ==================== 查询方法 ====================

    @GetMapping("/task/{taskId}")
    @Operation(summary = "查询任务的日志列表", description = "查询指定任务的所有日志记录")
    public Result<List<TaskLogVO>> getTaskLogs(
            @Parameter(description = "任务ID") @PathVariable String taskId) {
        String userId = UserContext.requireUserId();
        List<TaskLogVO> result = taskLogService.getTaskLogs(taskId, userId);
        return Result.success(result);
    }

    @GetMapping("/my/operations")
    @Operation(summary = "查询我的操作日志", description = "查询当前用户的所有操作日志")
    public Result<PageResult<TaskLogVO>> getMyOperationLogs(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        String userId = UserContext.requireUserId();
        PageResult<TaskLogVO> result = taskLogService.getUserOperationLogs(userId, pageNum, pageSize);
        return Result.success(result);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取日志统计信息", description = "获取任务日志的统计信息")
    public Result<Object> getLogStatistics(
            @Parameter(description = "任务ID") @RequestParam(required = false) String taskId,
            @Parameter(description = "日志类型") @RequestParam(required = false) Integer logType) {
        String userId = UserContext.requireUserId();
        
        // TODO: 实现日志统计逻辑
        
        return Result.success("日志统计功能待实现");
    }

    @GetMapping("/recent")
    @Operation(summary = "获取最近的日志", description = "获取最近的任务日志记录")
    public Result<List<TaskLogVO>> getRecentLogs(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit,
            @Parameter(description = "日志类型") @RequestParam(required = false) Integer logType) {
        String userId = UserContext.requireUserId();
        
        TaskLogQuery query = new TaskLogQuery();
        query.setLogType(logType);
        query.setCurrent(1);
        query.setSize(limit);
        
        PageResult<TaskLogVO> result = taskLogService.getTaskLogPage(query, userId);
        return Result.success(result.getRecords());
    }

    @GetMapping("/export")
    @Operation(summary = "导出任务日志", description = "导出指定条件的任务日志")
    public Result<String> exportTaskLogs(
            @Parameter(description = "任务ID") @RequestParam(required = false) String taskId,
            @Parameter(description = "开始时间") @RequestParam(required = false) String startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) String endTime,
            @Parameter(description = "导出格式") @RequestParam(defaultValue = "excel") String format) {
        String userId = UserContext.requireUserId();
        
        // TODO: 实现日志导出逻辑
        
        return Result.success("日志导出功能待实现");
    }
}
